package com.wang.lottery.infrastucture.repository;

import cn.bugstack.middleware.db.router.annotation.DBRouter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.lottery.domain.activity.model.po.UserTakeActivity;
import com.wang.lottery.domain.activity.model.po.UserTakeActivityCount;
import com.wang.lottery.domain.activity.model.vo.ActivityBillVO;
import com.wang.lottery.domain.activity.repository.IUserTakeActivityRepository;
import com.wang.lottery.infrastucture.mapper.UserStrategyExportMapper;
import com.wang.lottery.infrastucture.mapper.UserTakeActivityMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author wjh
 * @create 2023-11-08 18:28
 * 用户参与活动仓储
 */
@Service
@Slf4j
public class UserTakeActivityRepository extends ServiceImpl<UserTakeActivityMapper, UserTakeActivity> implements IUserTakeActivityRepository {
    
    @Resource
    private UserTakeActivityMapper userTakeActivityMapper;
    @Resource
    private UserStrategyExportMapper userStrategyExportMapper;
    

    /**
     * 领取活动(user_take_activity增加数据)
     *
     * @param activityBillVO 封装参数
     * @param takeId       领取id
     * @param takeDate     领取时间
     * @param takeCount    用户已领取次数
     */
    @Override
    public void takeActivity(ActivityBillVO activityBillVO, Long takeId, Date takeDate, Integer takeCount, Long strategyId) {
        UserTakeActivity userTakeActivity = new UserTakeActivity();
        userTakeActivity.setActivityId(activityBillVO.getActivityId());
        userTakeActivity.setuId(activityBillVO.getuId());
        userTakeActivity.setTakeId(takeId);
        userTakeActivity.setActivityName(activityBillVO.getActivityName());
        userTakeActivity.setTakeDate(takeDate);
        userTakeActivity.setTakeCount(takeCount);
        userTakeActivity.setUuid(activityBillVO.getuId() + "-" + activityBillVO.getActivityId() + "-" + takeCount);
        userTakeActivity.setCreateTime(new Date());
        userTakeActivity.setUpdateTime(userTakeActivity.getCreateTime());
        userTakeActivity.setStrategyId(strategyId);
        userTakeActivity.setState(false);
        userTakeActivityMapper.insert(userTakeActivity);
    }

    @Override
    public UserTakeActivityCount queryUserTakeActivityCount(String uId, Long activityId) {
        return null;
    }

    /**
     * 锁定活动领取记录
     *
     * @param uId        用户ID
     * @param activityId 活动ID
     * @return 更新结果
     */
    @Override
    public boolean lockTackActivity(String uId, Long activityId) {
        
        LambdaUpdateWrapper<UserTakeActivity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserTakeActivity::getuId, uId).eq(UserTakeActivity::getActivityId, activityId)
                .eq(UserTakeActivity::getState, 0)
               .set(UserTakeActivity::getState, 1);
        return this.update(updateWrapper);
    }



    /**
     * 查询是否存在未执行抽奖领取活动单【user_take_activity 存在 state = 0，领取了但抽奖过程失败的，可以直接返回领取结果继续抽奖】
     *
     * @param activityId 活动ID
     * @param uId        用户ID
     * @return 领取单
     */
    @Override
    @DBRouter
    public UserTakeActivity queryNoConsumedTakeActivityOrder(Long activityId, String uId) {
        LambdaQueryWrapper<UserTakeActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserTakeActivity::getuId, uId).eq(UserTakeActivity::getActivityId, activityId)
               .eq(UserTakeActivity::getState, 0);
        UserTakeActivity userTakeActivity = userTakeActivityMapper.selectOne(queryWrapper);
        if (userTakeActivity!= null) {
            return userTakeActivity;
        }
        return null;
    }


}
